//
//  ProfileViewController.m
//  cliqset
//
//  Created by David Hinson on 2/25/09.
//  Copyright 2009 Sumner Systems Management, Inc.. All rights reserved.
//

#import "ProfileViewController.h"
#import "cliqsetAppDelegate.h"
#import "Event.h"

@implementation ProfileViewController

@synthesize profile;
@synthesize links;
@synthesize hoverView;
@synthesize statusView;
@synthesize header;
@synthesize myName;
@synthesize myJob;

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];

	UIColor* myOrange			= [UIColor colorWithRed:249.0/255.0 green:163.0/255.0 blue:14.0/255.0 alpha:1.0];
	
	cliqsetAppDelegate* myApp = (cliqsetAppDelegate*)[[UIApplication sharedApplication] delegate];
	UINavigationController* nc;
	
	for( UIViewController* vc in [myApp tabBarController].viewControllers ) {
		NSString* testString = (NSString*)vc.title;
		if ([testString isEqualToString:@"Profile"]) {
			nc = (UINavigationController *)vc;
			nc.navigationBar.tintColor = myOrange;
			break;
		}
	}
	
	if (nc == nil) {return;}

	self.links                   = [[NSMutableArray alloc] init];
	
	//[myApp.profileEvents removeAllObjects];
	//[self reloadData];
	
	[self loadHeader];
	
    hoverView.center = [self view].center;
	
	[[self view] addSubview:hoverView];
    [[self view] bringSubviewToFront:hoverView];
	
	[self showHoverView:YES];

    statusView.center = [self view].center;
	
	CGRect newRect = CGRectMake(statusView.frame.origin.x, statusView.frame.origin.y - 20.0, statusView.frame.size.width, statusView.frame.size.height);
	
	statusView.frame = newRect;
	
	statusView.parent = (UIViewController*)self;
	
	[[self view] addSubview:statusView];
    [[self view] bringSubviewToFront:statusView];
	
	[self showStatusView:NO];
	
	if ([myApp.profileEvents count] == 0) {
		[myApp.cliqset loadProfileEvents:myApp.userString reload:YES];
		}
}

- (void)showStatusView:(BOOL)show
{
	// fade animate the view out of view by affecting its alpha
	[UIView beginAnimations:nil context:NULL];
	[UIView setAnimationDuration:0.40];
    
	if (show)
	{
		statusView.alpha = 0.90;
		[statusView.statusField becomeFirstResponder];
	}
	else
	{
		statusView.alpha = 0.0;
		[statusView.statusField resignFirstResponder];
	}
	
	[UIView commitAnimations];
}

- (void)showHoverView:(BOOL)show
{
	// fade animate the view out of view by affecting its alpha
	[UIView beginAnimations:nil context:NULL];
	[UIView setAnimationDuration:0.40];
    
	if (show)
	{
		hoverView.alpha = 0.85;
	}
	else
	{
		hoverView.alpha = 0.0;
	}
	
	[UIView commitAnimations];
}

// Load table header
-(void)loadHeader {
	
	cliqsetAppDelegate* myApp = (cliqsetAppDelegate*)[[UIApplication sharedApplication] delegate];	
	
	NSData* imageData;
	UIImage * graphic;
	UIImageView *showImage;
	
	self.header    = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)] retain];
	UIColor* textColor = [UIColor colorWithRed:85.0/255.0 green:85.0/255.0 blue:85.0/255.0 alpha:1.0];
	
	// Fonts for Labels
	UIFont* teeny_font      = [UIFont systemFontOfSize:14];
	UIFont* bold_large_font = [UIFont boldSystemFontOfSize:18];
	
	imageData = myApp.currentProfile.basicProfile.avatarBlob;
	graphic = (UIImage*)[[UIImage alloc] initWithData:imageData];
	
	// Allocate image
	showImage        = [[UIImageView alloc] initWithFrame:CGRectMake(230.0, 10.0, 80.0, 80.0 * (graphic.size.height / graphic.size.width))];
	showImage.opaque = NO;    
	showImage.image  = graphic;
	
	// Add picture to the cell
	[self.header addSubview:showImage];
	
	[showImage release];
	[graphic release];
	
	// Allocate Labels
	self.myName = [[[UILabel alloc] initWithFrame:CGRectMake(10.0, 10.0, 190.0, 20.0)] retain];
	self.myJob  = [[[UILabel alloc] initWithFrame:CGRectMake(10.0, 30.0, 190.0, 60.0)] retain];
	
	// Set name label
	self.myName.text = [@"" stringByAppendingFormat:@"%@ %@", myApp.currentProfile.basicProfile.givenName, myApp.currentProfile.basicProfile.familyName];
	self.myName.font = bold_large_font;
	
	// Set job label
	// myJob.text    = myApp.currentProfile.basicProfile.aboutMe;
	myJob.text       = myApp.currentStatus;
	self.myJob.font  = teeny_font;
	
	// Set label background color
	self.myName.backgroundColor = [UIColor clearColor];
	self.myJob.backgroundColor  = [UIColor clearColor];
	
	// Set text color
	self.myName.textColor       = textColor;
	self.myJob.textColor        = textColor;
	
	self.myJob.numberOfLines    = 0;
	self.myJob.lineBreakMode    = UILineBreakModeTailTruncation;
	
	// Add labels to the cell
	[header addSubview:self.myName];
	[header addSubview:self.myJob];
	
	self.profile.tableHeaderView = header;
}

#pragma mark ---- table handling ----

-(void)reloadData {
	[self.profile reloadData];
}

// Number of table sections
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
	return 1;
}

// Return rows in table
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	cliqsetAppDelegate* myApp = (cliqsetAppDelegate*)[[UIApplication sharedApplication] delegate];
	
	return [myApp.profileEvents count];
}



// Set row height
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
	cliqsetAppDelegate* myApp = (cliqsetAppDelegate*)[[UIApplication sharedApplication] delegate];
	UIFont* teeny_font        = [UIFont systemFontOfSize:14];
	
	if ([myApp.profileEvents count] == 0) {return 53.0;}
	
	Event * event = [myApp.profileEvents objectAtIndex:indexPath.row];
	
	if ([event.eventType isEqualToString:@"RelationshipEstablished"] ) {
		return 50;
	}
	
	if ([event.eventType isEqualToString:@"ProfileUpdated"]) {
		return 30;
	}
	
	if ([event.eventType isEqualToString:@"StatusUpdated"] || [event.eventType isEqualToString:@"NotePosted"] || [event.eventType isEqualToString:@"LinkPosted"]) {
		CGSize constraintSize;
		constraintSize.width = 220.0f;
		constraintSize.height = MAXFLOAT;
		
		CGSize offset1    = [[@"" stringByAppendingFormat:@"\"%@\"", event.body] sizeWithFont:teeny_font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeTailTruncation];
		return offset1.height + 40;
	}
	
	if ([event.eventType isEqualToString:@"ImagesPosted"]) {
		CGSize constraintSize;
		constraintSize.width = 220.0f;
		constraintSize.height = MAXFLOAT;
		
		CGSize offset1    = [[@"" stringByAppendingFormat:@"\"%@\"", event.body] sizeWithFont:teeny_font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeTailTruncation];
		return offset1.height + 90;
	}
	
    return 53.0;
}

// Draw cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
	NSData      *imageData;
	UIImageView *showImage;
	UILabel     *nameLabel;
	UILabel     *andLabel;
	UILabel     *nameLabel2;
	UILabel     *actionLabel;
	
	cliqsetAppDelegate* myApp = (cliqsetAppDelegate*)[[UIApplication sharedApplication] delegate];
    
	UITableViewCell *cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:nil] autorelease];
    [cell backgroundView].opaque=YES;
    [cell backgroundView].clearsContextBeforeDrawing;
	
   	[cell setSelectionStyle:UITableViewCellSelectionStyleNone];	
	// cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
	
	UIColor* textColor = [UIColor colorWithRed:85.0/255.0 green:85.0/255.0 blue:85.0/255.0 alpha:1.0];	
	UIColor* myBGColor = [UIColor colorWithRed:248.0/255.0 green:248.0/255.0 blue:248.0/255.0 alpha:1.0];
	
	Event * event = [myApp.profileEvents objectAtIndex:indexPath.row];
	
	// Fonts for Labels
	UIFont* teeny_font           = [UIFont systemFontOfSize:14];
	UIFont* bold_teeny_font      = [UIFont boldSystemFontOfSize:14];
	UIFont* bold_large_font      = [UIFont boldSystemFontOfSize:14];
	
	// Profile updated
	if ([event.eventType isEqualToString:@"ProfileUpdated"]) {
		UIImage *graphic = [UIImage imageNamed:@"id_card_edit.png"];
		showImage        = [[UIImageView alloc] initWithFrame:CGRectMake(5.0, ((30-(16 * graphic.size.height / graphic.size.width))/2.0), 16, 16 * (graphic.size.height / graphic.size.width))];		
		showImage.image  = graphic;
		
		// Allocate labels
		nameLabel      = [[UILabel alloc] initWithFrame:CGRectMake(26.0, 4, 250.0, 20.0)];
		
		// Build Name Label
		nameLabel.text = [@"" stringByAppendingFormat:@"%@ %@", event.sourceUser.givenName, event.sourceUser.familyName];
		
		CGSize constraintSize;
		constraintSize.width = 260.0f;
		constraintSize.height = MAXFLOAT;
		
		CGSize offset1                   = [nameLabel.text sizeWithFont:bold_teeny_font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeTailTruncation];
		CGSize offset2                   = [@" updated their profile." sizeWithFont:teeny_font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeTailTruncation];
		
		actionLabel    = [[UILabel alloc] initWithFrame:CGRectMake(26.0 + offset1.width, 4, offset2.width, 20.0)];
		
		nameLabel.font = bold_teeny_font;
		
		//if (event.sourceUser.gender == nil) {
		actionLabel.text  = @" updated their profile.";
		//	} 
		if ([event.sourceUser.gender isEqualToString:@"Male"]) {
			actionLabel.text  = @" updated his profile.";
			} 
		if ([event.sourceUser.gender isEqualToString:@"Female"]) {
			actionLabel.text  = @" updated her profile.";
			}
		actionLabel.font  = teeny_font;
		} 
	
	// Other added
	if ([event.eventType isEqualToString:@"OtherAdded"]) {
		UIImage *graphic = [UIImage imageNamed:@"user1_information.png"];
		showImage        = [[UIImageView alloc] initWithFrame:CGRectMake(5.0, ((30-(16 * graphic.size.height / graphic.size.width))/2.0), 16, 16 * (graphic.size.height / graphic.size.width))];		
		showImage.image  = graphic;
		
		// Allocate labels
		nameLabel      = [[UILabel alloc] initWithFrame:CGRectMake(26.0, 4, 250.0, 20.0)];
		
		// Build Name Label
		nameLabel.text = [@"" stringByAppendingFormat:@"%@ %@", event.sourceUser.givenName, event.sourceUser.familyName];
		
		CGSize constraintSize;
		constraintSize.width = 260.0f;
		constraintSize.height = MAXFLOAT;
		
		CGSize offset1    = [nameLabel.text sizeWithFont:bold_teeny_font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeTailTruncation];
		CGSize offset2    = [event.body sizeWithFont:teeny_font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeTailTruncation];
		
		actionLabel       = [[UILabel alloc] initWithFrame:CGRectMake(26.0, 4 + offset1.height, offset2.width, 20.0)];		
		nameLabel.font    = bold_teeny_font;
		
		actionLabel.text  = event.body;
		actionLabel.font  = teeny_font;
	} 
	
	// Other Updated
	if ([event.eventType isEqualToString:@"OtherUpdated"]) {
		UIImage *graphic = [UIImage imageNamed:@"user1_information.png"];
		showImage        = [[UIImageView alloc] initWithFrame:CGRectMake(5.0, ((30-(16 * graphic.size.height / graphic.size.width))/2.0), 16, 16 * (graphic.size.height / graphic.size.width))];		
		showImage.image  = graphic;
		
		// Allocate labels
		nameLabel      = [[UILabel alloc] initWithFrame:CGRectMake(26.0, 4, 250.0, 20.0)];		
		// Build Name Label
		nameLabel.text = [@"" stringByAppendingFormat:@"%@ %@", event.sourceUser.givenName, event.sourceUser.familyName];
		
		CGSize constraintSize;
		constraintSize.width = 260.0f;
		constraintSize.height = MAXFLOAT;
		
		CGSize offset1    = [nameLabel.text sizeWithFont:bold_teeny_font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeTailTruncation];
		CGSize offset2    = [event.body sizeWithFont:teeny_font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeTailTruncation];
		
		actionLabel       = [[UILabel alloc] initWithFrame:CGRectMake(26.0, 4 + offset1.height, offset2.width, 20.0)];		
		nameLabel.font    = bold_teeny_font;		
		actionLabel.text  = event.body;
		actionLabel.font  = teeny_font;
	} 
	
	// Status updated
	if ([event.eventType isEqualToString:@"StatusUpdated"]) {
		imageData         = event.sourceUser.avatarBlob;
		UIImage * graphic = (UIImage*)[[UIImage alloc] initWithData:imageData];
		showImage         = [[UIImageView alloc] initWithFrame:CGRectMake(5.0, ((54-(40 * graphic.size.height / graphic.size.width))/2.0), 40.0, 40.0 * (graphic.size.height / graphic.size.width))];		
		showImage.image   = graphic;
		[graphic release];
		
		// Allocate labels
		nameLabel      = [[UILabel alloc] initWithFrame:CGRectMake(50.0,  4, 240.0, 20.0)];

		CGSize constraintSize;
		constraintSize.width = 220.0f;
		constraintSize.height = MAXFLOAT;
		
		CGSize offset1    = [[@"" stringByAppendingFormat:@"\"%@\"", event.body] sizeWithFont:teeny_font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeTailTruncation];
				
		actionLabel    = [[UILabel alloc] initWithFrame:CGRectMake(50.0, 24, offset1.width,  offset1.height)];
		
		nameLabel.font = bold_large_font;
		
		// Build Name Label
		nameLabel.text            = [@"" stringByAppendingFormat:@"%@ %@", event.sourceUser.givenName, event.sourceUser.familyName];
		actionLabel.text          = [@"" stringByAppendingFormat:@"\"%@\"", event.body];
		actionLabel.font          = teeny_font;
		actionLabel.numberOfLines = 0;
		actionLabel.lineBreakMode = UILineBreakModeWordWrap;
		
		UIImageView * sourceImage = [[UIImageView alloc] initWithFrame:CGRectMake(274, 4, 16, 16)];
		NSData * sourceImageData  = event.sourceService.iconBlob;
		UIImage * graphic2        = (UIImage*)[[UIImage alloc] initWithData:sourceImageData];
		sourceImage.image         = graphic2;
		
		[cell addSubview:sourceImage];
		[graphic2 release];
		[sourceImage release];
	}
	
	// Note Posted
	if ([event.eventType isEqualToString:@"NotePosted"]) {
		UIImage *graphic = [UIImage imageNamed:@"note_pinned.png"];
		showImage        = [[UIImageView alloc] initWithFrame:CGRectMake(5.0, ((30-(16 * graphic.size.height / graphic.size.width))/2.0), 16, 16 * (graphic.size.height / graphic.size.width))];		
		showImage.image  = graphic;
		
		// Allocate labels
		nameLabel      = [[UILabel alloc] initWithFrame:CGRectMake(26.0,  4,  250.0, 20.0)];
		
		CGSize constraintSize;
		constraintSize.width = 240.0f;
		constraintSize.height = MAXFLOAT;
		
		CGSize offset1 = [[@"" stringByAppendingFormat:@"\"%@\"", event.body] sizeWithFont:teeny_font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeTailTruncation];
		
		actionLabel    = [[UILabel alloc] initWithFrame:CGRectMake(26.0, 24, offset1.width,  offset1.height)];
		
		nameLabel.font = bold_large_font;
		
		// Build Name Label
		nameLabel.text            = [@"" stringByAppendingFormat:@"%@ %@", event.sourceUser.givenName, event.sourceUser.familyName];
		actionLabel.text          = [@"" stringByAppendingFormat:@"\"%@\"", event.body];
		actionLabel.font          = teeny_font;
		actionLabel.numberOfLines = 0;
		
		UIImageView * sourceImage = [[UIImageView alloc] initWithFrame:CGRectMake(274, ((30-(16 * graphic.size.height / graphic.size.width))/2.0), 16, 16)];
		NSData * sourceImageData  = event.sourceService.iconBlob;
		UIImage * graphic2        = (UIImage*)[[UIImage alloc] initWithData:sourceImageData];
		sourceImage.image         = graphic2;
		
		[cell addSubview:sourceImage];
		[graphic2 release];
		[sourceImage release];
	}
	
	if ([event.eventType isEqualToString:@"ImagesPosted"]) {
		UIImage *graphic = [UIImage imageNamed:@"photo_scenery.png"];
		showImage        = [[UIImageView alloc] initWithFrame:CGRectMake(5.0, ((30-(16 * graphic.size.height / graphic.size.width))/2.0), 16, 16 * (graphic.size.height / graphic.size.width))];
		showImage.image  = graphic;
		
		// Allocate labels
		nameLabel      = [[UILabel alloc] initWithFrame:CGRectMake(26.0,  4,  250.0, 20.0)];
		
		CGSize constraintSize;
		constraintSize.width = 240.0f;
		constraintSize.height = MAXFLOAT;
		
		CGSize offset1 = [[@"" stringByAppendingFormat:@"\"%@\"", event.body] sizeWithFont:teeny_font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeTailTruncation];
		
		actionLabel    = [[UILabel alloc] initWithFrame:CGRectMake(26.0, 24, offset1.width,  offset1.height)];
		
		nameLabel.font = bold_large_font;
		
		// Build Name Label
		nameLabel.text            = [@"" stringByAppendingFormat:@"%@ %@", event.sourceUser.givenName, event.sourceUser.familyName];
		actionLabel.text          = [@"" stringByAppendingFormat:@"\"%@\"", event.body];
		actionLabel.font          = teeny_font;
		actionLabel.numberOfLines = 0;
		
		UIImageView * sourceImage = [[UIImageView alloc] initWithFrame:CGRectMake(274, ((30-(16 * graphic.size.height / graphic.size.width))/2.0), 16, 16)];
		NSData * sourceImageData  = event.sourceService.iconBlob;
		UIImage * graphic2        = (UIImage*)[[UIImage alloc] initWithData:sourceImageData];
		sourceImage.image         = graphic2;
		
		[cell addSubview:sourceImage];
		[graphic2 release];
		[sourceImage release];
		
		NSMutableString * link1  = @"";
		NSMutableString * link2  = @"";
		NSMutableString * link3  = @"";
		NSMutableString * link4  = @"";
		NSMutableString * link5  = @"";
		NSMutableData * thumb1   = nil;
		NSMutableData * thumb2   = nil;
		NSMutableData * thumb3   = nil;
		NSMutableData * thumb4   = nil;
		NSMutableData * thumb5   = nil;
		
		for (int x=0; x<[event.resources count]; x++) {
			Resource * resource = [event.resources objectAtIndex:x];
			if ([resource.name isEqualToString:@"Link-1"]) {
				link1 = resource.value;
			}
			if ([resource.name isEqualToString:@"Link-2"]) {
				link2 = resource.value;
			}
			if ([resource.name isEqualToString:@"Link-3"]) {
				link3 = resource.value;
			}
			if ([resource.name isEqualToString:@"Link-4"]) {
				link4 = resource.value;
			}
			if ([resource.name isEqualToString:@"Link-5"]) {
				link5 = resource.value;
			}
			if ([resource.name isEqualToString:@"Thumbnail-1"]) {
				thumb1 = resource.blob;
			}
			if ([resource.name isEqualToString:@"Thumbnail-2"]) {
				thumb2 = resource.blob;
			}
			if ([resource.name isEqualToString:@"Thumbnail-3"]) {
				thumb3 = resource.blob;
			}
			if ([resource.name isEqualToString:@"Thumbnail-4"]) {
				thumb4 = resource.blob;
			}
			if ([resource.name isEqualToString:@"Thumbnail-5"]) {
				thumb5 = resource.blob;
			}
		}
		
		// Setup links
		if (![link1 isEqualToString:@""]) {
			[self.links addObject:link1];
			
			UIButton * linkButton1 = [[UIButton alloc] initWithFrame:CGRectMake(26.0, 53.0, 40.0, 40.0)];
			[linkButton1 setBackgroundImage:[UIImage imageWithData:(NSData*)thumb1] forState:UIControlStateNormal];		
			[linkButton1 addTarget:self action:@selector(doLink:) forControlEvents:UIControlEventTouchUpInside];
			linkButton1.tag = [links count];
			[links addObject:link1];
			
			[cell addSubview:linkButton1];
			[linkButton1 release];
		}
		if (![link2 isEqualToString:@""]) {
			[self.links addObject:link2];
			
			UIButton * linkButton2 = [[UIButton alloc] initWithFrame:CGRectMake(76.0, 53.0, 40.0, 40.0)];
			[linkButton2 setBackgroundImage:[UIImage imageWithData:thumb2] forState:UIControlStateNormal];		
			[linkButton2 addTarget:self action:@selector(doLink:) forControlEvents:UIControlEventTouchUpInside];
			linkButton2.tag = [links count];
			[links addObject:link2];
			
			[cell addSubview:linkButton2];
			[linkButton2 release];
		}
		
		if (![link3 isEqualToString:@""]) {
			[self.links addObject:link3];
			
			UIButton * linkButton3 = [[UIButton alloc] initWithFrame:CGRectMake(126.0, 53.0, 40.0, 40.0)];
			[linkButton3 setBackgroundImage:[UIImage imageWithData:thumb3] forState:UIControlStateNormal];		
			[linkButton3 addTarget:self action:@selector(doLink:) forControlEvents:UIControlEventTouchUpInside];
			linkButton3.tag = [links count];
			[links addObject:link3];
			
			[cell addSubview:linkButton3];
			[linkButton3 release];
		}
		
		if (![link4 isEqualToString:@""]) {
			[self.links addObject:link4];
			
			UIButton * linkButton4 = [[UIButton alloc] initWithFrame:CGRectMake(176.0, 53.0, 40.0, 40.0)];
			[linkButton4 setBackgroundImage:[UIImage imageWithData:thumb4] forState:UIControlStateNormal];		
			[linkButton4 addTarget:self action:@selector(doLink:) forControlEvents:UIControlEventTouchUpInside];
			linkButton4.tag = [links count];
			[links addObject:link4];
			
			[cell addSubview:linkButton4];
			[linkButton4 release];
		}
		
		if (![link5 isEqualToString:@""]) {
			[self.links addObject:link5];
			
			UIButton * linkButton5 = [[UIButton alloc] initWithFrame:CGRectMake(226.0, 53.0, 40.0, 40.0)];
			[linkButton5 setBackgroundImage:[UIImage imageWithData:thumb5] forState:UIControlStateNormal];		
			[linkButton5 addTarget:self action:@selector(doLink:) forControlEvents:UIControlEventTouchUpInside];
			linkButton5.tag = [links count];
			[links addObject:link5];
			
			[cell addSubview:linkButton5];
			[linkButton5 release];
		}
	}
	
	// Link posted
	if ([event.eventType isEqualToString:@"LinkPosted"]) {
		UIImage *graphic = [UIImage imageNamed:@"link.png"];
		showImage        = [[UIImageView alloc] initWithFrame:CGRectMake(5.0, 5, 16, 16 * (graphic.size.height / graphic.size.width))];		
		showImage.image  = graphic;
		
		// Allocate labels
		nameLabel      = [[UILabel alloc] initWithFrame:CGRectMake(26.0,  4,  250.0, 20.0)];
		
		CGSize constraintSize;
		constraintSize.width = 240.0f;
		constraintSize.height = MAXFLOAT;
		
		CGSize offset1 = [[@"" stringByAppendingFormat:@"\"%@\"", event.body] sizeWithFont:teeny_font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeTailTruncation];
		
		actionLabel    = [[UILabel alloc] initWithFrame:CGRectMake(26.0, 24, offset1.width,  offset1.height)];
		
		nameLabel.font = bold_large_font;
		
		// Build Name Label
		nameLabel.text            = [@"" stringByAppendingFormat:@"%@ %@", event.sourceUser.givenName, event.sourceUser.familyName];
		actionLabel.text          = [@"" stringByAppendingFormat:@"\"%@\"", event.body];
		actionLabel.font          = teeny_font;
		actionLabel.numberOfLines = 0;
		
		UIImageView * sourceImage = [[UIImageView alloc] initWithFrame:CGRectMake(274, ((30-(16 * graphic.size.height / graphic.size.width))/2.0), 16, 16)];
		NSData * sourceImageData  = event.sourceService.iconBlob;
		UIImage * graphic2        = (UIImage*)[[UIImage alloc] initWithData:sourceImageData];
		sourceImage.image         = graphic2;
		
		[cell addSubview:sourceImage];
		[graphic2 release];
		[sourceImage release];
		
		NSString* description = @"Unknown";
		NSString* link        = @"Unknown";
		
		CGSize offset3;
		
		for (int x=0; x<[event.resources count]; x++) {
			Resource * resource = [event.resources objectAtIndex:x];
			if ([resource.name isEqualToString:@"Description"]) {
				description = resource.value;
			}
			if ([resource.name isEqualToString:@"Link"]) {
				link = resource.value;
			}
		}
		
		offset3     = [description sizeWithFont:teeny_font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeTailTruncation];
		UILabel * linkLabel       = [[UILabel alloc] initWithFrame:CGRectMake(26.0, cell.frame.size.height - 2,  offset3.width, 20.0)];		
		UIButton * linkButton     = [[UIButton alloc] initWithFrame:CGRectMake(26.0, cell.frame.size.height - 2, offset3.width, 20.0)];
		
		[linkButton addTarget:self action:@selector(doLink:) forControlEvents:UIControlEventTouchUpInside];
		linkButton.tag = [links count];
		[links addObject:link];
		
		linkLabel.backgroundColor = [UIColor clearColor];
		linkLabel.textColor       = [UIColor blueColor];
		linkLabel.text            = description;
		linkLabel.font            = teeny_font;
		
		[cell addSubview:linkButton];
		[linkButton release];
		
		[cell addSubview:linkLabel];
		[linkLabel release];
	}
	
	if ([event.eventType isEqualToString:@"RelationshipEstablished"]) {
		UIImage *graphic = [UIImage imageNamed:@"users3_add.png"];
		showImage        = [[UIImageView alloc] initWithFrame:CGRectMake(5.0, ((30-(16 * graphic.size.height / graphic.size.width))/2.0), 16, 16 * (graphic.size.height / graphic.size.width))];		
		showImage.image  = graphic;
		
		// Allocate labels
		nameLabel      = [[UILabel alloc] initWithFrame:CGRectMake(26.0, 4, 250.0, 20.0)];
		
		NSMutableString * name = (NSMutableString*)[@"" stringByAppendingFormat:@"%@ %@", event.sourceUser.givenName, event.sourceUser.familyName];
		
		// Build Name Label
		nameLabel.text = name;
		
		CGSize constraintSize;
		constraintSize.width = 260.0f;
		constraintSize.height = MAXFLOAT;
		
		CGSize offset1    = [name sizeWithFont:bold_teeny_font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeTailTruncation];
		CGSize offset2    = [@"and " sizeWithFont:teeny_font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeTailTruncation];
		
		andLabel                 = [[UILabel alloc] initWithFrame:CGRectMake(offset1.width + offset2.width, 4, offset2.width, 20.0)];
		andLabel.text            = @"and ";
		andLabel.font            = teeny_font;
		andLabel.textColor       = textColor;
		andLabel.backgroundColor = [UIColor clearColor];
		
		CGSize offset3           = [andLabel.text sizeWithFont:teeny_font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeTailTruncation];
		CGSize offset4           = [event.body sizeWithFont:bold_teeny_font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeTailTruncation];
		CGSize offset5           = [@" established a relationship." sizeWithFont:teeny_font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeTailTruncation];
		
		nameLabel2                 = [[UILabel alloc] initWithFrame:CGRectMake(offset1.width + offset2.width + offset3.width, 4, offset4.width, 20.0)];
		nameLabel2.font            = bold_teeny_font;
		nameLabel2.textColor       = textColor;
		nameLabel2.backgroundColor = [UIColor clearColor];
		nameLabel2.text            = event.body;
		
		actionLabel    = [[UILabel alloc] initWithFrame:CGRectMake(26, 4 + offset1.height, offset5.width, 20.0)];
		
		nameLabel.font = bold_teeny_font;
		
		[cell addSubview:andLabel];
		[cell addSubview:nameLabel2];
		
		[andLabel release];
		[nameLabel2 release];
		
		actionLabel.text  = @" established a relationship.";
		actionLabel.font  = teeny_font;
	}
	
    showImage.opaque          = NO;    
	showImage.backgroundColor = myBGColor;
	
	nameLabel.backgroundColor        = [UIColor clearColor];
	actionLabel.backgroundColor      = [UIColor clearColor];
	cell.contentView.backgroundColor = myBGColor;
	
	// Set text color
	nameLabel.textColor       = textColor;
	actionLabel.textColor     = textColor;
	cell.textColor            = textColor;
	
	// Set label in cell
	[cell addSubview:nameLabel];		
	// Set label in cell
	[cell addSubview:actionLabel];	
	// Add image to cell
	[cell addSubview:showImage];
	
	// Cleanup our junk
	[showImage release];
	[nameLabel release];
	[actionLabel release];
	
	// if we are on row zero, we are done and should hide the loading screen
	if (indexPath.row == 0) {
		[self showHoverView:NO];    
	}
	
	return cell;
}	

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
    // Release anything that's not essential, such as cached data
}

// Refresh
-(IBAction)doRefresh:(id)id {
	NSLog(@"Refresh");
	cliqsetAppDelegate* myApp = (cliqsetAppDelegate*)[[UIApplication sharedApplication] delegate];
	[self showHoverView:YES];
	[myApp.cliqset loadProfileEvents:myApp.userString reload:YES];
}

// Post status to cliqset
-(IBAction)doStatus:(id)id {
	NSLog(@"doStatus");
	
	[self showStatusView:(statusView.alpha == 0.0)];
}

- (void)dealloc {
	[header release];
	[myName release];
	[myJob release];
	[statusView release];
	[hoverView release];
	[links release];
	[profile release];
    [super dealloc];
}


@end
